package org.muninn.niagara.structures;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.baja.control.BControlPoint;
import javax.baja.sys.BComponent;

public class BControllerNode extends BPointTreeNode
{
  public BControllerNode(BComponent comp) { super(comp); this.construct(comp); }
  
  public void construct(BComponent comp)
  {
    if (this.isNull()) return;
    
    BComponent[] temp = comp.getChildComponents();
    
    for (int i = 0; i < temp.length; i++)
    {
      if (temp[i] instanceof BControlPoint)
        this.add(new BPointNode(temp[i]));
      
      else
        this.construct(temp[i]);
    }
  }
  
  public BPointNode[] getControllersLike(String search)
  {
    BPointNode[] pts = new BPointNode[this.children.length];
    search.replaceAll("x", "\\\\d+");
    search = search + "\\d*";
    Pattern pat = Pattern.compile(search);
    
    for (int i = 0, k = 0; i < this.children.length; i++)
    {
      Matcher match = pat.matcher(children[i].name);
      boolean found = false;
      while (match.find()) { found = true; }
      if (found)
        pts[k] = (BPointNode) children[i];
    }
    
    return pts;
  }
  
  public BPointNode getPoint(String name)
  {
    for (int i = 0; i < this.children.length; i++)
    {
      if (this.children[i].name.equals(name))
        return (BPointNode) this.children[i];
    }
    return null;
  }
  
  public BPointNode[] getAllPoints() { return (BPointNode[]) this.children; }
  
}
